import storage from 'store'
import { getCurrentUserInfo } from '@/api/login'
// import { fetchAppInfo } from '@/api/login'
import { ACCESS_TOKEN, CURRENT_APP } from '@/store/mutation-types'

const user = {
    state: {
        token: '',
        name: '',
        avatar: '',
        roles: [],
        info: {}
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_INFO: (state, info) => {
            state.info = info
        }
    },

    actions: {
        // 登录
        Login({ commit }, userInfo) {
            console.log(commit)
            console.log(userInfo)
            return new Promise((resolve, reject) => {
                resolve()
                reject()
                    // login(userInfo).then(response => {
                    //     const result = response.result
                    //     storage.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
                    //     commit('SET_TOKEN', result.token)
                    //     resolve()
                    // }).catch(error => {
                    //     reject(error)
                    // })
            })
        },

        // 获取用户信息
        GetInfo({ commit }) {
            return new Promise((resolve, reject) => {
                getCurrentUserInfo().then(response => {
                    const { data, code } = response
                    if (code === 200) {
                        commit('SET_NAME', data.nickName)
                        commit('SET_AVATAR', data.photoUrl)
                        commit('SET_TOKEN', storage.get(ACCESS_TOKEN))
                        resolve(response)
                    } else {
                        reject(new Error('请重新登录 !'))
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取关联的公众号
        // GetUserApps({ commit }) {
        //     return new Promise((resolve, reject) => {
        //         fetchAppInfo().then(response => {

        //         })
        //     })
        // },

        // 设置用户信息
        SetInfo({ commit }, userInfo) {
            commit('SET_NAME', { name: userInfo.nickName })
            commit('SET_AVATAR', userInfo.avatar)
            commit('SET_TOKEN', userInfo.token)
            storage.set(ACCESS_TOKEN, userInfo.token, 7 * 24 * 60 * 60 * 1000)
        },

        // 登出
        Logout({ commit }) {
            return new Promise((resolve) => {
                commit('SET_TOKEN', '')
                commit('SET_ROLES', [])
                storage.remove(ACCESS_TOKEN)
                storage.remove(CURRENT_APP)
                resolve()
            })
        }

    }
}

export default user